package org.zfes.snowier.zbss.xzy.biz.device.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZObjectUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;
import org.zfes.snowier.zbss.xzy.biz.device.dao.DeviceTypeMapper;
import org.zfes.snowier.zbss.xzy.biz.device.model.DeviceModel;
import org.zfes.snowier.zbss.xzy.biz.device.model.DeviceType;
import org.zfes.snowier.zbss.xzy.biz.device.service.IDeviceModelService;
import org.zfes.snowier.zbss.xzy.biz.device.service.IDeviceTypeService;
import org.zfes.snowier.zbss.xzy.device.DeviceModelDto;
import org.zfes.snowier.zbss.xzy.device.DeviceTypeDto;

import com.google.common.collect.Lists;

import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class DeviceTypeServiceImpl implements IDeviceTypeService{
 	@Autowired
	private DeviceTypeMapper deviceTypeMapper;
	@Autowired
	private IDeviceModelService deviceModelService;
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createDeviceType(DeviceType deviceType) {
		ZBeanUtil.setCreateInfo(deviceType);
		ZBeanUtil.validateBean(deviceType);
		deviceTypeMapper.insertSelective(deviceType);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<DeviceType> loadDeviceTypeById(Long id) {
		if(id!=null){
			return Optional.ofNullable(deviceTypeMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateDeviceType(DeviceType deviceType) {
		ZBeanUtil.setUpdatedInfo(deviceType);
		ZBeanUtil.validateBean(deviceType);
		deviceTypeMapper.updateByPrimaryKeySelective(deviceType);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteDeviceType(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		deviceTypeMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setDeviceTypeStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		
		if(Byte.valueOf("0").equals(status)) {
			deviceModelService.setStatusByTypeId(id, status);
		}
		deviceTypeMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadDeviceTypeDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(deviceTypeMapper.selectListCount(pm), deviceTypeMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<DeviceType> loadAllDeviceType(Byte status) {
		return deviceTypeMapper.selectAll(status);
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<DeviceTypeDto> loadAllDeviceTypeModel(Byte status) {
		List<DeviceType> deviceTypeList = deviceTypeMapper.selectAll(status);
		if(ZObjectUtil.isEmpty(deviceTypeList)) {
			return Lists.newArrayList();
		}
		List<DeviceTypeDto> dataList=deviceTypeList.stream().map(v->{
			DeviceTypeDto dto=new DeviceTypeDto();
			ZBeanUtil.copy(v, dto);
			return dto;
		}).collect(Collectors.toList());
		
		List<DeviceModel>  modelList = deviceModelService.loadByDeviceTypeList(deviceTypeList.stream().map(DeviceType::getId).collect(Collectors.toList()), status);
		
		dataList.forEach(type->{
			
			type.setDeviceModelList(
					modelList.stream()
					.filter(model -> type.getId().equals(model.getTypeId()))
					.map(model->{
						DeviceModelDto mDto=new DeviceModelDto();
						ZBeanUtil.copy(model, mDto);
						return mDto;
					})
					.collect(Collectors.toList()));
		});
		
		
		return dataList;
	}
	
}